#pragma once
#include <array>
#include <qjsonobject.h>

using u8 = unsigned char;
using u16 = unsigned short;
using u32 = unsigned int;
using u64 = unsigned long long;

struct SocketAddress
{
    u16 family;
    u16 port;
    union 
    {
        u32 u_addr;
        std::array<u8,4> b_addr;
    };
    std::array<u8,8> m_zero;

    operator struct sockaddr*()
    {
        return (struct sockaddr*)this;
    }

    bool init_any_addr(u16 p);
    bool init_any_port(const char* addr_str,u16 p);
};

using CaptionType = std::array<char,256>;

enum NODE_TYPE : u32
{
    NODE_TYPE_UDP_IN = 0x0001,
    NODE_TYPE_UDP_OUT = 0x0002,
    NODE_TYPE_TCP_CLIENT_IN = 0x0004,
    NODE_TYPE_TCP_CLIENT_OUT = 0x0008,
    NODE_TYPE_TCP_SERVER_IN = 0x0010,
    NODE_TYPE_TCP_SERVER_OUT = 0x0020,
    NODE_TYPE_FILE_IN = 0x0040,
    NODE_TYPE_INPUT_IN = 0x0080,
    NODE_TYPE_LINE = 0x0100,
};

constexpr u32 INPUT_NODE_MASK = NODE_TYPE_UDP_IN | NODE_TYPE_TCP_CLIENT_IN | NODE_TYPE_TCP_SERVER_IN | NODE_TYPE_FILE_IN | NODE_TYPE_INPUT_IN;
constexpr u32 OUT_NODE_MASK = NODE_TYPE_UDP_OUT | NODE_TYPE_TCP_CLIENT_OUT | NODE_TYPE_TCP_SERVER_OUT;

struct NodePoint
{
    int x;
    int y;
};

struct NodeCreateInfo
{
    u32 id;
    NODE_TYPE node_type;
    CaptionType caption;
    CaptionType note;
    SocketAddress addr;
    NodePoint point;
    u32 out_id;
};

QJsonObject serialize_node_create_info(NodeCreateInfo& info);
bool load_node_create_info(QJsonObject& object,NodeCreateInfo& out_info);